package org.jeecg.modules.flower.service.impl;

import org.jeecg.modules.flower.entity.FlowerChannel;
import org.jeecg.modules.flower.entity.FlowerAccount;
import org.jeecg.modules.flower.mapper.FlowerAccountMapper;
import org.jeecg.modules.flower.mapper.FlowerChannelMapper;
import org.jeecg.modules.flower.service.IFlowerChannelService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 渠道管理
 * @Author: jeecg-boot
 * @Date:   2022-09-21
 * @Version: V1.0
 */
@Service
public class FlowerChannelServiceImpl extends ServiceImpl<FlowerChannelMapper, FlowerChannel> implements IFlowerChannelService {

	@Autowired
	private FlowerChannelMapper flowerChannelMapper;
	@Autowired
	private FlowerAccountMapper flowerAccountMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(FlowerChannel flowerChannel, List<FlowerAccount> flowerAccountList) {
		flowerChannelMapper.insert(flowerChannel);
		for(FlowerAccount entity:flowerAccountList) {
			//外键设置
			entity.setChannelId(flowerChannel.getId());
			flowerAccountMapper.insert(entity);
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(FlowerChannel flowerChannel,List<FlowerAccount> flowerAccountList) {
		flowerChannelMapper.updateById(flowerChannel);
		
		//1.先删除子表数据
		flowerAccountMapper.deleteByMainId(flowerChannel.getId());
		
		//2.子表数据重新插入
		for(FlowerAccount entity:flowerAccountList) {
			//外键设置
			entity.setChannelId(flowerChannel.getId());
			flowerAccountMapper.insert(entity);
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		flowerAccountMapper.deleteByMainId(id);
		flowerChannelMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			flowerAccountMapper.deleteByMainId(id.toString());
			flowerChannelMapper.deleteById(id);
		}
	}
	
}
